﻿/*
 * DotNetCrypt - an open source library of cryptographic algorithms for .NET
 * Copyright (C) 2009 David Musgrove
 * 
 * This file is part of DotNetCrypt.
 *
 * DotNetCrypt is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DotNetCrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace DotNetCrypt
{
    /// <summary>
    /// Computes the <see cref="SHA224" /> hash for the input data using this
    /// managed library. 
    /// </summary>
// ReSharper disable InconsistentNaming
    public sealed class SHA224Managed : SHA224
// ReSharper restore InconsistentNaming
    {
        private uint[] _internalState;
        static private readonly uint[] _roundConstants = new uint[] { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 };

        /// <summary>
        /// Initializes a new instance of the <see cref="SHA224Managed" />
        /// class using the managed library.
        /// </summary>
        public SHA224Managed()
        {
            InitialiseState();
        }

        private void InitialiseState()
        {
            _internalState = new uint[8];
            _internalState[0] = 0xc1059ed8;
            _internalState[1] = 0x367cd507;
            _internalState[2] = 0x3070dd17;
            _internalState[3] = 0xf70e5939;
            _internalState[4] = 0xffc00b31;
            _internalState[5] = 0x68581511;
            _internalState[6] = 0x64f98fa7;
            _internalState[7] = 0xbefa4fa4;
            Count = 0;
        }

        /// <summary>
        /// 
        /// Initializes an implementation of the
        /// <see cref="T:System.Security.Cryptography.HashAlgorithm"/> class.
        /// </summary>
        public override void Initialize()
        {
            InitialiseState();
        }

        /// <summary>
        /// When implemented in a derived class, transforms a single block of
        /// bytes using the chosen hash algorithm.
        /// </summary>
        /// <param name="array">
        /// An array containing the block of bytes.
        /// </param>
        /// <param name="ibStart">
        /// The offset of the start of the block within the array.
        /// </param>
        protected override void TransformBlock(byte[] array, int ibStart)
        {
            var workingValues = new uint[64];

            uint h0 = _internalState[0];
            uint h1 = _internalState[1];
            uint h2 = _internalState[2];
            uint h3 = _internalState[3];
            uint h4 = _internalState[4];
            uint h5 = _internalState[5];
            uint h6 = _internalState[6];
            uint h7 = _internalState[7];

            for (int i = 0; i < 64; i++)
            {
                uint temp1;
                if (i < 16)
                {
                    temp1 = ((uint)array[ibStart++] << 24) |
                        ((uint)array[ibStart++] << 16) |
                        ((uint)array[ibStart++] << 8) |
                        array[ibStart++];
                }
                else
                {
                    temp1 = workingValues[i - 2];
                    uint temp2 = workingValues[i - 15];
                    temp1 = ((temp1 >> 17) | (temp1 << 15)) ^
                        ((temp1 >> 19) | (temp1 << 13)) ^
                        (temp1 >> 10);
                    temp2 = ((temp2 >> 7) | (temp2 << 25)) ^
                        ((temp2 >> 18) | (temp2 << 14)) ^
                        (temp2 >> 3);
                    temp1 = temp1 + workingValues[i - 7] + temp2 + workingValues[i - 16];
                }
                workingValues[i] = temp1;
                uint t1 = h7 + (((h4 >> 6) | (h4 << 26)) ^ ((h4 >> 11) | (h4 << 21)) ^ ((h4 >> 25) | (h4 << 7))) +
                          ((h4 & (h5 ^ h6)) ^ h6) +
                          _roundConstants[i] + workingValues[i];
                uint t2 = (((h0 >> 2) | (h0 << 30)) ^ ((h0 >> 13) | (h0 << 19)) ^ ((h0 >> 22) | (h0 << 10))) +
                          (((h0 ^ h1) & h2) ^ (h0 & h1));
                h7 = h6;
                h6 = h5;
                h5 = h4;
                h4 = h3 + t1;
                h3 = h2;
                h2 = h1;
                h1 = h0;
                h0 = t1 + t2;
            }

            _internalState[0] += h0;
            _internalState[1] += h1;
            _internalState[2] += h2;
            _internalState[3] += h3;
            _internalState[4] += h4;
            _internalState[5] += h5;
            _internalState[6] += h6;
            _internalState[7] += h7;
        }

        /// <summary>
        /// Finalizes the hash computation after the last data is processed
        /// by the cryptographic stream object.
        /// </summary>
        /// <returns>
        /// The computed hash code.
        /// </returns>
        protected override byte[] HashFinal()
        {
            var block = new byte[0x1c];
            int bytesRemaining = 0x40 - ((int)(Count & 0x3fL));
            int blockSize = 0x40;
            if (bytesRemaining <= 8)
            {
                blockSize += 0x40;
            }
            var partIn = new byte[blockSize];
            FinalBlock.CopyTo(partIn, 0);
            partIn[FinalBlock.Length] = 0x80;
            long num2 = Count * 8L;
            partIn[blockSize - 8] = (byte)((num2 >> 0x38) & 0xffL);
            partIn[blockSize - 7] = (byte)((num2 >> 0x30) & 0xffL);
            partIn[blockSize - 6] = (byte)((num2 >> 40) & 0xffL);
            partIn[blockSize - 5] = (byte)((num2 >> 0x20) & 0xffL);
            partIn[blockSize - 4] = (byte)((num2 >> 0x18) & 0xffL);
            partIn[blockSize - 3] = (byte)((num2 >> 0x10) & 0xffL);
            partIn[blockSize - 2] = (byte)((num2 >> 8) & 0xffL);
            partIn[blockSize - 1] = (byte)(num2 & 0xffL);
            Count = blockSize;
            HashCore(partIn, 0, blockSize);
            Utils.WriteWordsIntoBytesBigEndian(_internalState, block);
            HashValue = block;
            return block;
        }
    }
}
